package com.google.zxing.client.android;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import doan.chuyennganh.activity.R;
import doan.chuyennganh.base.DoAnBaseAcitvity;
import doan.chuyennganh.model.ListStudent;
import doan.chuyennganh.util.Constants;

@SuppressLint("SimpleDateFormat")
public class ZxingMainActivity extends DoAnBaseAcitvity{
	private ArrayList<String> checkList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scanner_activity);
		((TextView) findViewById(R.id.tv_scanner_complete)).setVisibility(View.GONE);
		Intent intent = getIntent();
		if(intent != null){
			checkList = intent.getStringArrayListExtra(Constants.EXTRAS_CHECK_LIST);
		}
		if(checkList == null)
		{
			checkList = new ArrayList<String>();
		}
		BeginBarCodeScanner();
	}
//	private String getTodayFormatString() {
//		Calendar c = Calendar.getInstance();
//		SimpleDateFormat df = new SimpleDateFormat("dd_MM_yyyy");
//		
//		String new_col = df.format(c.getTime());
//
//		int Hr24 = c.get(Calendar.HOUR_OF_DAY);
//
//		if (Hr24 < 12) {
//			new_col = "Sang_" + new_col;
//		} else {
//			new_col = "Chieu_" + new_col;
//		}
//
//		return new_col;
//	}

	private void BeginBarCodeScanner() {
		Intent scannerIntent = new Intent(
				"com.google.zxing.client.android.SCAN");
		scannerIntent.putExtra("SCAN_MODE", "CODE_128");
		startActivityForResult( scannerIntent, Constants.SCANNER_REQUEST_CODE);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

		if (requestCode == Constants.SCANNER_REQUEST_CODE) {
			boolean isDone = false;
			if (resultCode == RESULT_OK) {

				String contents = intent.getStringExtra(Intents.Scan.RESULT);
				isDone = intent.getBooleanExtra("DONE", false);

				
				if(!ListStudent.getInstance().isContains(contents)){
					
					enableDialogWarning(this, getString(R.string.txt_warning_dialog_title), String.format( getString(R.string.txt_warning_student_not_found), contents), true);
					
					return;
				}
				if (!checkList.contains(contents)) {
					checkList.add(contents);
				} else {
					Toast.makeText(this,
							getString(R.string.txt_warning_scanned),
							Toast.LENGTH_LONG).show();
				}

			} else {
				isDone = true;
			}
			if (!isDone) {
				BeginBarCodeScanner();
			} else {
				Intent data = new  Intent();
//				checkList.add("111111");
//				checkList.add("222222");
				data.putExtra(Constants.EXTRAS_CHECK_LIST, checkList);
				setResult(RESULT_OK, data);
				finish();
			}
		}
	}
}
